package com.seeyon.ctp.rest.resources;

import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSONObject;
//import com.seeyon.apps.zhongh.controller.HttpUtil;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.ctp.organization.principal.PrincipalManager;
import com.seeyon.ctp.services.security.ServiceManager;
import com.seeyon.ctp.util.JDBCAgent;
import com.seeyon.ctp.util.annotation.RestInterfaceAnnotation;


@Path("org")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class SrmUnitCode extends BaseResource{
    private static Log log = LogFactory.getLog(SrmUnitCode.class);

    //http://localhost/seeyon/rest/org/getUnitCode 根据华辉登录名获取NC登录名
    @Path("getUnitCode")
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    //@getUnitCode
    public Response getNCLoginName(Map data){
        log.info("getNCLoginName init----");
        String UNITNAME = data.get("loginName").toString();
        String sql="select UNITCODE from DEF_SRM_UNITCODE where UNITNAME = '"+UNITNAME+"'";
        JDBCAgent agent = new JDBCAgent();
        ResultSet result = null;
        try{
            agent.execute(sql);
            result = agent.getQueryResult();
            while(result.next()){
                return ok("{\"code\":\"1\",\"message\":\""+result.getString("UNITCODE")+"\"}");
            }
            return ok("{\"code\":\"0\",\"message\":\"未获取到对应编码！\"}");
        }catch(Exception e){
            log.info("内容出错："+e.getMessage()+","+JSONObject.toJSONString(e.getStackTrace()));
        }finally{
            try {
                result.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            agent.close();
        }
        return ok("{\"code\":\"0\",\"message\":\"获取编码失败！\"}");
    }

}